package com.example.dds.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.dds.datasource.DBIdentifier;
import com.example.dds.dto.*;
import com.example.dds.mapper.ArticleMapper;
import com.example.dds.tiancai.Base64Utils;
import com.example.dds.tiancai.DESUtil;
import org.apache.tomcat.util.codec.binary.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 用户数据访问接口。
 * @author xielc
 * @version 2018年12月26日
 */
@RestController
@RequestMapping(value="/getDB")
public class WSRest{

    @Autowired
    ArticleMapper articleMapper;

    private int sum;

    /**
     *查询项目中所有用户信息
     *@param projectCode 项目编码
     *@return 用户列表
     */
    @RequestMapping(value="/v1", method= RequestMethod.GET)
    public String queryUser(@RequestParam(value="projectCode", required=true) String projectCode,String id) {
        DBIdentifier.setProjectCode(projectCode);
        //Article article = articleMapper.selectByPrimaryKey(id);
        long startTime = System.currentTimeMillis();    //获取开始时间
        List<String> list = articleMapper.selectOrder();
        long endTime = System.currentTimeMillis();    //获取结束时间
        System.out.println("程序运行时间：" + (endTime - startTime) + "ms");    //输出程序运行时间
        return "xxx";
    }

    /**
     *天财对接
     *@param projectCode 项目编码
     *@return 订单列表
     */
    @RequestMapping(value="/v2", method= RequestMethod.GET)
    public String queryOrder(@RequestParam(value="projectCode", required=true) String projectCode) throws Exception {
        DBIdentifier.setProjectCode(projectCode);
        long startTime = System.currentTimeMillis();    //获取开始时间
        List<Shop> shops = articleMapper.selectShops();
        shops.forEach(p->{
                List<Order> list = articleMapper.selectTianCaiOrders("1",p.getShopId());
                sum=sum+list.size();
                if(list!=null && !list.isEmpty()){
                    TableOrder tableOrder = new TableOrder();
                    JSONArray records = new JSONArray();
                    list.forEach(s->{
                        records.add(new String[]{s.getStore_name(),s.getBrand_name(),s.getCity(),s.getAddress(),s.getSettle_biz_date(), s.getOpen_time(),String.valueOf(s.getBusi_income()),String.valueOf(s.getRecv_money()),String.valueOf(s.getItem_disc_money_sum()),String.valueOf(s.getBill_disc_money_sum()),String.valueOf(s.getReal_income()),String.valueOf(s.getCancel_money()),s.getSale_type_name(),s.getStore_code(),s.getGroup_code(),s.getGroup_name(),s.getO2o_store_code(),s.getO2o_group_code(),s.getBrand_code(),s.getRegion(),s.getProvince(),s.getCounty(),s.getArea(),s.getOrder_source_name(),s.getOrder_type_name(),s.getTax_money(),s.getDining_type_name(),s.getSystem_resource(),s.getCode(),s.getTable_code(),s.getTable_display_name(),s.getTable_def_capacity(),s.getTable_open_time(),s.getTable_open_shift_id(),s.getTable_open_shift_name(),s.getTable_settle_time(),s.getSettle_date(),s.getSettle_code(), s.getSale_type_id(),s.getOrder_origin_id(),s.getDining_type_id(),s.getDining_mode_name(),s.getManage_type(),s.getRemark(),s.getId()});
                    });
                    String[] columnNames = new String[]{"store_name","brand_name","city","address","settle_biz_date", "open_time", "busi_income", "recv_money","item_disc_money_sum","bill_disc_money_sum", "real_income","cancel_money", "sale_type_name", "store_code","group_code" , "group_name", "o2o_store_code", "o2o_group_code" , "brand_code" , "region" , "province", "county", "area" ,"order_source_name","order_type_name","tax_money", "dining_type_name" , "system_resource" , "code" , "table_code" , "table_display_name" , "table_def_capacity" ,"table_open_time","table_open_shift_id","table_open_shift_name", "table_settle_time" ,"settle_date", "settle_code" , "sale_type_id" , "order_origin_id" , "dining_type_id" , "dining_mode_name" , "manage_type" , "remark","id"};
                    tableOrder.setTableName("账单事实表_新");
                    tableOrder.setKeyCol("store_code,group_code,id");
                    tableOrder.setColumnNames(columnNames);
                    tableOrder.setRecords(records);
                    //压缩
                    File file = null;
                    try {
                       file = File.createTempFile("["+p.getShopName()+"账单事实表_新"+"(数量："+list.size()+")]", ".zip");
//                       file = File.createTempFile("[大丸百货外卖账单事实表2019-6(数量："+list.size()+")]", ".zip");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    ZipOutputStream zos = null;
                    try {
                        zos = new ZipOutputStream(new FileOutputStream(file));
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    //加密后压缩
                    String desKey = "c8b3c46746f770a7";
                    try {
                        createZip(zos, Base64Utils.encode(DESUtil.encrypt(JSONObject.toJSONString(tableOrder),desKey)).getBytes("UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    try {
                        zos.closeEntry();
                        zos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    System.out.println("------------>"+file);
                }

            /*List<String> dates = articleMapper.selectDate();
            dates.forEach(d->{
                List<Order> list = articleMapper.selectTianCaiOrders(d,p.getShopId());
                sum=sum+list.size();
                System.out.println("----------->"+p.getShopName()+"账单事实表_新"+d+"(数量："+list.size()+")");
//                List<Order> list = articleMapper.selectTianCaiOrders("1","2");
                *//*if(list!=null && !list.isEmpty()){
                    TableOrder tableOrder = new TableOrder();
                    JSONArray records = new JSONArray();
                    list*//**//*.subList(0,2)*//**//*.forEach(s->{
                        records.add(new String[]{s.getStore_name(),s.getBrand_name(),s.getCity(),s.getAddress(),s.getSettle_biz_date(), s.getOpen_time(),String.valueOf(s.getBusi_income()),String.valueOf(s.getRecv_money()),String.valueOf(s.getItem_disc_money_sum()),String.valueOf(s.getBill_disc_money_sum()),String.valueOf(s.getReal_income()),String.valueOf(s.getCancel_money()),s.getSale_type_name(),s.getStore_code(),s.getGroup_code(),s.getGroup_name(),s.getO2o_store_code(),s.getO2o_group_code(),s.getBrand_code(),s.getRegion(),s.getProvince(),s.getCounty(),s.getArea(),s.getOrder_source_name(),s.getOrder_type_name(),s.getTax_money(),s.getDining_type_name(),s.getSystem_resource(),s.getCode(),s.getTable_code(),s.getTable_display_name(),s.getTable_def_capacity(),s.getTable_open_time(),s.getTable_open_shift_id(),s.getTable_open_shift_name(),s.getTable_settle_time(),s.getSettle_date(),s.getSettle_code(), s.getSale_type_id(),s.getOrder_origin_id(),s.getDining_type_id(),s.getDining_mode_name(),s.getManage_type(),s.getRemark(),s.getId()});
                    });
                    String[] columnNames = new String[]{"store_name","brand_name","city","address","settle_biz_date", "open_time", "busi_income", "recv_money","item_disc_money_sum","bill_disc_money_sum", "real_income","cancel_money", "sale_type_name", "store_code","group_code" , "group_name", "o2o_store_code", "o2o_group_code" , "brand_code" , "region" , "province", "county", "area" ,"order_source_name","order_type_name","tax_money", "dining_type_name" , "system_resource" , "code" , "table_code" , "table_display_name" , "table_def_capacity" ,"table_open_time","table_open_shift_id","table_open_shift_name", "table_settle_time" ,"settle_date", "settle_code" , "sale_type_id" , "order_origin_id" , "dining_type_id" , "dining_mode_name" , "manage_type" , "remark","id"};
                    tableOrder.setTableName("账单事实表_新");
                    tableOrder.setKeyCol("store_code,group_code,id");
                    tableOrder.setColumnNames(columnNames);
                    tableOrder.setRecords(records);
                    //压缩
                    File file = null;
                    try {
                       file = File.createTempFile("["+p.getShopName()+"外卖账单事实表_新"+d+"(数量："+list.size()+")]", ".zip");
//                       file = File.createTempFile("[大丸百货外卖账单事实表2019-6(数量："+list.size()+")]", ".zip");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    ZipOutputStream zos = null;
                    try {
                        zos = new ZipOutputStream(new FileOutputStream(file));
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    //加密后压缩
                    String desKey = "c8b3c46746f770a7";
                    try {
                        createZip(zos, Base64Utils.encode(DESUtil.encrypt(JSONObject.toJSONString(tableOrder),desKey)).getBytes("UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    try {
                        zos.closeEntry();
                        zos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    System.out.println("------------>"+file);
                }*//*
           });*/
        });
        long endTime = System.currentTimeMillis();    //获取结束时间
        System.out.println("程序运行时间：" + (endTime - startTime) + "ms");    //输出程序运行时间
        System.out.println("--------->sum"+sum);
        return "Order";
    }

    /**
     *天财对接
     *@param projectCode 项目编码
     *@return 订单详情列表
     */
    @RequestMapping(value="/v3", method= RequestMethod.GET)
    public String queryOrderItem(@RequestParam(value="projectCode", required=true) String projectCode) throws Exception {
        DBIdentifier.setProjectCode(projectCode);
        long startTime = System.currentTimeMillis();    //获取开始时间
        List<Shop> shops = articleMapper.selectShops();
        shops.forEach(p->{
            List<String> dates = articleMapper.selectDate();
            dates.forEach(d->{
                SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM");
                try {
                    for(int i =1 ;i<=getDaysOfMonth(sdf.parse(d));i++){
                        List<OrderItem> list = articleMapper.TianCaiOrderItem((i<10)?d+"-0"+i:d+"-"+i,p.getShopId());
//                      List<OrderItem> list = articleMapper.TianCaiOrderItem("1","2");
                        sum=sum+list.size();
                        System.out.println("----------->"+p.getShopName()+"账单明细事实表_新"+d+"(数量："+list.size()+")");
                        /*if(list!=null && !list.isEmpty()){
                            TableOrder tableOrder = new TableOrder();
                            JSONArray records = new JSONArray();
                            list.forEach(s->{
                                records.add(new String[]{s.getSettle_biz_date(),s.getLast_qty() , s.getItem_name(),String.valueOf(s.getStandard_price()),String.valueOf(s.getLast_price()),String.valueOf(s.getPre_discount_price()),String.valueOf(s.getDiscount_amount_total()),String.valueOf(s.getShare_not_income_money()),String.valueOf(s.getActual_money()),String.valueOf(s.getIncome_money()),s.getMeal_flg(),s.getMeal_name(),s.getStore_name(),s.getGroup_name(),s.getBrand_code(),s.getBrand_name(),s.getCity(),String.valueOf(s.getReturn_money()),s.getReturn_num(),s.getTable_open_shift_name(),s.getItem_category_name(),s.getItem_sub_category_name(),String.valueOf(s.getItem_standard_price()),String.valueOf(s.getMeal_raise_price()),s.getIs_meal_raise_price_by_qty(),s.getPeople_qty(),s.getGroup_code(),s.getStore_code(),s.getProvince(),s.getCounty(),s.getOpen_time(),s.getItem_id(),s.getItem_type(),s.getCode(),s.getAction_id(),s.getTable_open_time(),s.getSettle_time(),s.getBill_type(),s.getSettle_date(),s.getOrig_qty(),s.getItem_category_code(),s.getItem_sub_category_code(), s.getSale_type_name(),s.getTable_name(),s.getTable_type_name(),s.getTable_def_capacity(),s.getManage_type(), s.getFood_format(),s.getItem_code(),s.getTax_money() ,s.getPlace_order_time() ,s.getItem_unit_name(),s.getAddbill_remark(),s.getBs_id(),s.getMemo(),s.getSystem_resource()});
                            });
                            String[] columnNames = new String[]{"settle_biz_date", "last_qty","item_name", "standard_price" , "last_price" , "pre_discount_price","discount_amount_total" , "share_not_income_money", "actual_money" , "income_money" , "meal_flg" , "meal_name" , "store_name" , "group_name" , "brand_code" , "brand_name" , "city" , "return_money" , "return_num" , "table_open_shift_name" ,"item_category_name" ,"item_sub_category_name" , "item_standard_price" , "meal_raise_price","is_meal_raise_price_by_qty" , "people_qty" ,"group_code" ,"store_code","province","county" , "open_time" , "item_id","item_type" , "code" , "action_id" , "table_open_time" , "settle_time" , "bill_type" , "settle_date" , "orig_qty" ,"item_category_code" ,"item_sub_category_code" , "sale_type_name" , "table_name","table_type_name" , "table_def_capacity" , "manage_type" , "food_format" , "item_code" , "tax_money" , "place_order_time" , "item_unit_name" , "addbill_remark","bs_id","memo","system_resource" };
                            tableOrder.setTableName("账单明细事实表_新");
                            tableOrder.setKeyCol("store_code,group_code,action_id,id");
                            tableOrder.setColumnNames(columnNames);
                            tableOrder.setRecords(records);
                            //压缩
                            File file = null;
                            try {
                                file = File.createTempFile("["+p.getShopName()+"账单明细事实表_新"+((i<10)?d+"-0"+i:d+"-"+i)+"(数量："+list.size()+")]", ".zip");
//                        file = File.createTempFile("[大丸百货外卖账单明细事实表2019-6(数量："+list.size()+")]", ".zip");
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            ZipOutputStream zos = null;
                            try {
                                zos = new ZipOutputStream(new FileOutputStream(file));
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }
                            //加密后压缩
                            String desKey = "c8b3c46746f770a7";
                            try {
                                createZip(zos, Base64Utils.encode(DESUtil.encrypt(JSONObject.toJSONString(tableOrder),desKey)).getBytes("UTF-8"));
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                            try {
                                zos.closeEntry();
                                zos.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            System.out.println("------------>"+file);
                        }*/
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            });
        });
        long endTime = System.currentTimeMillis();    //获取结束时间
        System.out.println("程序运行时间：" + (endTime - startTime) + "ms");    //输出程序运行时间
        System.out.println("--------------->sum:"+sum);
        return "OrderItem";
    }

    /**
     *天财对接
     *@param projectCode 项目编码
     *@return 支付详情列表
     */
    @RequestMapping(value="/v4", method= RequestMethod.GET)
    public String queryOrderPaymentItem(@RequestParam(value="projectCode", required=true) String projectCode) throws Exception {
        DBIdentifier.setProjectCode(projectCode);
        long startTime = System.currentTimeMillis();    //获取开始时间
        List<Shop> shops = articleMapper.selectShops();
        shops.forEach(p->{
            List<OrderPaymentItem> list = articleMapper.TianCaiOrderPaymentItem("",p.getShopId());
//                List<OrderPaymentItem> list = articleMapper.TianCaiOrderPaymentItem("1","2");
            sum=sum+list.size();
            if(list!=null && !list.isEmpty()){
                TableOrder tableOrder = new TableOrder();
                JSONArray records = new JSONArray();
                list.forEach(s->{
                    records.add(new String[]{s.getSettle_biz_date(),s.getSettlement_type_name(),String.valueOf(s.getPay_money()),String.valueOf(s.getReceipt_money()),String.valueOf(s.getNon_income()),String.valueOf(s.getReal_income()),String.valueOf(s.getIncome()),s.getStore_name(),s.getGroup_name(),s.getBrand_name() , s.getCity(),s.getPeople_qty(),s.getStore_code(),s.getGroup_code(),s.getBrand_code(),s.getRegion(),s.getProvince(),s.getCounty(),s.getOpen_time(),s.getManage_type(),s.getSettle_id(), s.getBill_code(),s.getTable_settle_shift_id(),s.getTable_settle_shift_name(),s.getTable_display_name(),s.getTable_type_name(),s.getTable_def_capacity(),s.getTable_open_time(),s.getTable_settle_time(),s.getSettle_date(),s.getSettle_state_id(),s.getSettle_state_name(),s.getSettlement_id(),s.getSettlement_name(),s.getSettlement_type_id(),s.getSale_type_name(),s.getBill_id(),s.getFood_format(),s.getSettle_payway_id(),s.getMemo(),s.getSystem_resource()});
                });
                String[] columnNames = new String[]{"settle_biz_date", "settlement_type_name", "pay_money" , "receipt_money" , "non_income" ,"real_income", "income" , "store_name" , "group_name" , "brand_name" , "city" , "people_qty" , "store_code" , "group_code" , "brand_code" , "region" , "province" , "county" , "open_time" , "manage_type" , "settle_id" , "bill_code" , "table_settle_shift_id" , "table_settle_shift_name" , "table_display_name" , "table_type_name" , "table_def_capacity" , "table_open_time" , "table_settle_time" , "settle_date" , "settle_state_id" , "settle_state_name" , "settlement_id" , "settlement_name" , "settlement_type_id" , "sale_type_name" , "bill_id" , "food_format" , "settle_payway_id","memo","system_resource" };
                tableOrder.setTableName("结算明细事实表_新");
                tableOrder.setKeyCol("store_code,group_code,settle_payway_id");
                tableOrder.setColumnNames(columnNames);
                tableOrder.setRecords(records);
                //压缩
                File file = null;
                try {
                    file = File.createTempFile("["+p.getShopName()+"结算明细事实表_新"+"(数量："+list.size()+")]", ".zip");
//                        file = File.createTempFile("[大丸百货外卖结算明细事实表2019-6(数量："+list.size()+")]", ".zip");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                ZipOutputStream zos = null;
                try {
                    zos = new ZipOutputStream(new FileOutputStream(file));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                //加密后压缩
                String desKey = "c8b3c46746f770a7";
                try {
                    createZip(zos, Base64Utils.encode(DESUtil.encrypt(JSONObject.toJSONString(tableOrder),desKey)).getBytes("UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                try {
                    zos.closeEntry();
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println("------------>"+file);
            }
            /*List<String> dates = articleMapper.selectDate();
            dates.forEach(d->{
                List<OrderPaymentItem> list = articleMapper.TianCaiOrderPaymentItem(d,p.getShopId());
//                List<OrderPaymentItem> list = articleMapper.TianCaiOrderPaymentItem("1","2");
                if(list!=null && !list.isEmpty()){
                    TableOrder tableOrder = new TableOrder();
                    JSONArray records = new JSONArray();
                    list.forEach(s->{
                        records.add(new String[]{s.getSettle_biz_date(),s.getSettlement_type_name(),String.valueOf(s.getPay_money()),String.valueOf(s.getReceipt_money()),String.valueOf(s.getNon_income()),String.valueOf(s.getReal_income()),String.valueOf(s.getIncome()),s.getStore_name(),s.getGroup_name(),s.getBrand_name() , s.getCity(),s.getPeople_qty(),s.getStore_code(),s.getGroup_code(),s.getBrand_code(),s.getRegion(),s.getProvince(),s.getCounty(),s.getOpen_time(),s.getManage_type(),s.getSettle_id(), s.getBill_code(),s.getTable_settle_shift_id(),s.getTable_settle_shift_name(),s.getTable_display_name(),s.getTable_type_name(),s.getTable_def_capacity(),s.getTable_open_time(),s.getTable_settle_time(),s.getSettle_date(),s.getSettle_state_id(),s.getSettle_state_name(),s.getSettlement_id(),s.getSettlement_name(),s.getSettlement_type_id(),s.getSale_type_name(),s.getBill_id(),s.getFood_format(),s.getSettle_payway_id(),s.getMemo(),s.getSystem_resource()});
                    });
                    String[] columnNames = new String[]{"settle_biz_date", "settlement_type_name", "pay_money" , "receipt_money" , "non_income" ,"real_income", "income" , "store_name" , "group_name" , "brand_name" , "city" , "people_qty" , "store_code" , "group_code" , "brand_code" , "region" , "province" , "county" , "open_time" , "manage_type" , "settle_id" , "bill_code" , "table_settle_shift_id" , "table_settle_shift_name" , "table_display_name" , "table_type_name" , "table_def_capacity" , "table_open_time" , "table_settle_time" , "settle_date" , "settle_state_id" , "settle_state_name" , "settlement_id" , "settlement_name" , "settlement_type_id" , "sale_type_name" , "bill_id" , "food_format" , "settle_payway_id","memo","system_resource" };
                    tableOrder.setTableName("结算明细事实表_新");
                    tableOrder.setKeyCol("store_code,group_code,settle_payway_id");
                    tableOrder.setColumnNames(columnNames);
                    tableOrder.setRecords(records);
                    //压缩
                    File file = null;
                    try {
                        file = File.createTempFile("["+p.getShopName()+"外卖结算明细事实表_新"+d+"(数量："+list.size()+")]", ".zip");
//                        file = File.createTempFile("[大丸百货外卖结算明细事实表2019-6(数量："+list.size()+")]", ".zip");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    ZipOutputStream zos = null;
                    try {
                        zos = new ZipOutputStream(new FileOutputStream(file));
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    //加密后压缩
                    String desKey = "c8b3c46746f770a7";
                    try {
                        createZip(zos, Base64Utils.encode(DESUtil.encrypt(JSONObject.toJSONString(tableOrder),desKey)).getBytes("UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    try {
                        zos.closeEntry();
                        zos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    System.out.println("------------>"+file);
                }
            });*/
        });
        long endTime = System.currentTimeMillis();    //获取结束时间
        System.out.println("程序运行时间：" + (endTime - startTime) + "ms");    //输出程序运行时间
        System.out.println("--------------->sum:"+sum);
        return "OrderPaymentItem";
    }

    /**
     *天财对接
     *@param projectCode 项目编码
     *@return 厨打列表
     */
    @RequestMapping(value="/v5", method= RequestMethod.GET)
    public String queryKitchen(@RequestParam(value="projectCode", required=true) String projectCode) throws Exception {
        DBIdentifier.setProjectCode(projectCode);
        long startTime = System.currentTimeMillis();    //获取开始时间
        List<Shop> shops = articleMapper.selectShops();
        shops.forEach(p->{
            List<Kitchen> list = articleMapper.TianCaiKitchen("",p.getShopId());
//                List<Kitchen> list = articleMapper.TianCaiKitchen("1","2");
            sum=sum+list.size();
            if(list!=null && !list.isEmpty()){
                TableOrder tableOrder = new TableOrder();
                JSONArray records = new JSONArray();
                list.forEach(s->{
                    records.add(new String[]{s.getGroup_code(),s.getStore_code(),s.getBs_id(),s.getItem_name(),s.getLast_qty(),s.getServe_finish_time(),s.getCreate_time(),s.getStore_name() ,s.getId(),s.getShop_remark(),s.getCreator_name()});
                });
                String[] columnNames = new String[]{"group_code" , "store_code" , "bs_id" , "item_name" , "last_qty" , "serve_finish_time" , "create_time" , "store_name" , "id" ,"shop_remark","creator_name"};
                tableOrder.setTableName("厨打信息表");
                tableOrder.setKeyCol("store_code,group_code,id");
                tableOrder.setColumnNames(columnNames);
                tableOrder.setRecords(records);
                //压缩
                File file = null;
                try {
                    file = File.createTempFile("["+p.getShopName()+"厨打信息表"+"(数量："+list.size()+")]", ".zip");
//                        file = File.createTempFile("[大丸百货外卖厨打信息表2019-6(数量："+list.size()+")]", ".zip");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                ZipOutputStream zos = null;
                try {
                    zos = new ZipOutputStream(new FileOutputStream(file));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                //加密后压缩
                String desKey = "c8b3c46746f770a7";
                try {
                    createZip(zos, Base64Utils.encode(DESUtil.encrypt(JSONObject.toJSONString(tableOrder),desKey)).getBytes("UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                try {
                    zos.closeEntry();
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println("------------>"+file);
            }
            /*List<String> dates = articleMapper.selectDate();
            dates.forEach(d->{
                List<Kitchen> list = articleMapper.TianCaiKitchen(d,p.getShopId());
//                List<Kitchen> list = articleMapper.TianCaiKitchen("1","2");
                if(list!=null && !list.isEmpty()){
                    TableOrder tableOrder = new TableOrder();
                    JSONArray records = new JSONArray();
                    list.forEach(s->{
                        records.add(new String[]{s.getGroup_code(),s.getStore_code(),s.getBs_id(),s.getItem_name(),s.getLast_qty(),s.getServe_finish_time(),s.getCreate_time(),s.getStore_name() ,s.getId(),s.getShop_remark(),s.getCreator_name()});
                    });
                    String[] columnNames = new String[]{"group_code" , "store_code" , "bs_id" , "item_name" , "last_qty" , "serve_finish_time" , "create_time" , "store_name" , "id" ,"shop_remark","creator_name"};
                    tableOrder.setTableName("厨打信息表");
                    tableOrder.setKeyCol("store_code,group_code,id");
                    tableOrder.setColumnNames(columnNames);
                    tableOrder.setRecords(records);
                    //压缩
                    File file = null;
                    try {
                        file = File.createTempFile("["+p.getShopName()+"外卖厨打信息表"+d+"(数量："+list.size()+")]", ".zip");
//                        file = File.createTempFile("[大丸百货外卖厨打信息表2019-6(数量："+list.size()+")]", ".zip");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    ZipOutputStream zos = null;
                    try {
                        zos = new ZipOutputStream(new FileOutputStream(file));
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    //加密后压缩
                    String desKey = "c8b3c46746f770a7";
                    try {
                        createZip(zos, Base64Utils.encode(DESUtil.encrypt(JSONObject.toJSONString(tableOrder),desKey)).getBytes("UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    try {
                        zos.closeEntry();
                        zos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    System.out.println("------------>"+file);
                }
            });*/
        });
        long endTime = System.currentTimeMillis();    //获取结束时间
        System.out.println("程序运行时间：" + (endTime - startTime) + "ms");    //输出程序运行时间
        System.out.println("--------------->sum:"+sum);
        return "Kitchen";
    }

    /**
     *天财对接
     *@param projectCode 项目编码
     *@return 会员消费
     */
    @RequestMapping(value="/v6", method= RequestMethod.GET)
    public String queryMemberConsum(@RequestParam(value="projectCode", required=true) String projectCode) throws Exception {
        DBIdentifier.setProjectCode(projectCode);
        long startTime = System.currentTimeMillis();    //获取开始时间
        List<Shop> shops = articleMapper.selectShops();
        shops.forEach(p->{
            List<String> dates = articleMapper.selectDate();
            dates.forEach(d->{
                List<MemberConsum> list = articleMapper.TianCaiMemberConsum(d,p.getShopId());
//                List<MemberConsum> list = articleMapper.TianCaiMemberConsum("1","2");
                if(list!=null && !list.isEmpty()){
                    TableOrder tableOrder = new TableOrder();
                    JSONArray records = new JSONArray();
                    list.forEach(s->{
                        records.add(new String[]{s.getGroup_name() ,s.getStore_code(), s.getCity(),s.getMem_sex(),s.getMem_res_name(), s.getMem_birthday(),String.valueOf(s.getConsume_bill_amount()), String.valueOf(s.getConsume_card_amount()),String.valueOf(s.getConsume_real_amount()), String.valueOf(s.getConsume_free_amount()), s.getConsume_coupon_num(),String.valueOf(s.getConsume_coupon_waste_amount()),s.getGroup_code() ,s.getStore_name(),s.getProvince() , s.getCounty(),String.valueOf(s.getConsume_cashback_amount()),String.valueOf(s.getConsume_coupon_amount()),String.valueOf(s.getConsume_coupon_real_amount()),s.getStore_create_time(),s.getMem_code(), s.getMem_name(),s.getMem_mobile(),s.getMem_store_code(), s.getMem_store_name(), s.getMem_create_time(),s.getMem_res(), s.getMem_birthday_lunar(),s.getMem_card_no(), s.getCode(),s.getConsume_time(),s.getConsume_res_name(), s.getMem_consume_type(),s.getConsume_date(),s.getSystem()});
                    });
                    String[] columnNames = new String[]{"group_name" , "store_code" , "city" , "mem_sex" , "mem_res_name" , "mem_birthday" , "consume_bill_amount" , "consume_card_amount" , "consume_real_amount" , "consume_free_amount" , "consume_coupon_num" , "consume_coupon_waste_amount" , "group_code" , "store_name" , "province" , "county" , "consume_cashback_amount" , "consume_coupon_amount" , "consume_coupon_real_amount" , "store_create_time" , "mem_code" , "mem_name" , "mem_mobile" , "mem_store_code" , "mem_store_name" , "mem_create_time" , "mem_res" , "mem_birthday_lunar" , "mem_card_no" , "code" , "consume_time" , "consume_res_name" , "mem_consume_type" , "consume_date","system" };
                    tableOrder.setTableName("会员消费信息事实表");
                    tableOrder.setKeyCol("store_code,group_code,code,consume_date");
                    tableOrder.setColumnNames(columnNames);
                    tableOrder.setRecords(records);
                    //压缩
                    File file = null;
                    try {
                          file = File.createTempFile("["+p.getShopName()+"会员消费信息事实表"+d+"(数量："+list.size()+")]", ".zip");
//                        file = File.createTempFile("[大丸百货会员消费事实表2019-6(数量："+list.size()+")]", ".zip");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    ZipOutputStream zos = null;
                    try {
                        zos = new ZipOutputStream(new FileOutputStream(file));
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    //加密后压缩
                    String desKey = "c8b3c46746f770a7";
                    try {
                        createZip(zos, Base64Utils.encode(DESUtil.encrypt(JSONObject.toJSONString(tableOrder),desKey)).getBytes("UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    try {
                        zos.closeEntry();
                        zos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    System.out.println("------------>"+file);
                }
            });
        });
        long endTime = System.currentTimeMillis();    //获取结束时间
        System.out.println("程序运行时间：" + (endTime - startTime) + "ms");    //输出程序运行时间
        return "MemberConsum";
    }

    /**
     *天财对接
     *@param projectCode 项目编码
     *@return 会员储值
     */
    @RequestMapping(value="/v7", method= RequestMethod.GET)
    public String queryMemberStored(@RequestParam(value="projectCode", required=true) String projectCode) throws Exception {
        DBIdentifier.setProjectCode(projectCode);
        long startTime = System.currentTimeMillis();    //获取开始时间
        List<Shop> shops = articleMapper.selectShops();
        shops.forEach(p->{
            List<String> dates = articleMapper.selectDate();
            dates.forEach(d->{
                List<MemberStored> list = articleMapper.TianCaiMemberStored(d,p.getShopId());
//                List<MemberStored> list = articleMapper.TianCaiMemberStored("1","2");
                if(list!=null && !list.isEmpty()){
                    TableOrder tableOrder = new TableOrder();
                    JSONArray records = new JSONArray();
                    list.forEach(s->{
                        records.add(new String[]{s.getGroup_name(),s.getStore_name(),s.getCity(),s.getMem_sex(),s.getMem_birthday(),String.valueOf(s.getRecharge_amount()),String.valueOf(s.getRecharge_real_amount()), String.valueOf(s.getRecharge_donate_amount()),s.getGroup_code(),s.getStore_code(),s.getProvince(),s.getCounty(),s.getMem_card_type_code(),s.getMem_card_type(),s.getRecharge_type(),s.getStore_create_time(),s.getMem_code() ,s.getMem_name(),s.getMem_mobile(),s.getMem_store_code(),s.getMem_store_name(),s.getMem_create_time(),s.getMem_res(),s.getMem_res_name(),s.getMem_birthday_lunar(),s.getMem_card_no(),s.getCode(),s.getRecharge_res_name(),s.getRecharge_time(),s.getRecharge_pay_type(),s.getRevoke_name(),s.getRecharge_date(),s.getSystem()});
                    });
                    String[] columnNames = new String[]{"group_name", "store_name" , "city" , "mem_sex" , "mem_birthday" , "recharge_amount" , "recharge_real_amount" , "recharge_donate_amount" , "group_code" , "store_code" , "province" , "county" , "mem_card_type_code" , "mem_card_type" , "recharge_type" , "store_create_time" , "mem_code" , "mem_name" , "mem_mobile" , "mem_store_code" , "mem_store_name" , "mem_create_time" , "mem_res" , "mem_res_name" , "mem_birthday_lunar" , "mem_card_no" , "code" , "recharge_res_name" , "recharge_time" , "recharge_pay_type" ,"revoke_name", "recharge_date","system" };
                    tableOrder.setTableName("会员储值信息事实表");
                    tableOrder.setKeyCol("store_code,group_code,code,recharge_date");
                    tableOrder.setColumnNames(columnNames);
                    tableOrder.setRecords(records);
                    //压缩
                    File file = null;
                    try {
                          file = File.createTempFile("["+p.getShopName()+"会员储值信息事实表"+d+"(数量："+list.size()+")]", ".zip");
//                        file = File.createTempFile("[大丸百货会员储值信息事实表2019-6(数量："+list.size()+")]", ".zip");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    ZipOutputStream zos = null;
                    try {
                        zos = new ZipOutputStream(new FileOutputStream(file));
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    //加密后压缩
                    String desKey = "c8b3c46746f770a7";
                    try {
                        createZip(zos, Base64Utils.encode(DESUtil.encrypt(JSONObject.toJSONString(tableOrder),desKey)).getBytes("UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    try {
                        zos.closeEntry();
                        zos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    System.out.println("------------>"+file);
                }
            });
        });
        long endTime = System.currentTimeMillis();    //获取结束时间
        System.out.println("程序运行时间：" + (endTime - startTime) + "ms");    //输出程序运行时间
        return "MemberStored";
    }

    private static void createZip(ZipOutputStream zos, byte[] b) {
        try {
            zos.putNextEntry(new ZipEntry("param.txt"));
            zos.setComment("by zip test!");
            zos.write(b, 0, b.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static int getDaysOfMonth(Date date){
         Calendar calendar = Calendar.getInstance();
         calendar.setTime(date);
         return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM");
        System.out.println(getDaysOfMonth(sdf.parse("2019-02")));
    }
}
